package com.logic.landseaserver.models.workorder.ws;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.WorkOrderAppraise;
import com.logic.landseaserver.models.workorder.bean.EffectiveProjectResp;
import com.logic.landseaserver.models.workorder.bean.WorkOrderDetailResp;
import com.logic.landseaserver.models.workorder.bean.WorkOrderListResp;
import com.logic.landseaserver.models.workorder.bean.WorkOrderReportListResp;
import com.logic.landseaserver.models.workorder.bean.WorkOrderReq;
import com.logic.landseaserver.models.workorder.service.WorkOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/workorder")
@Api(value = "工单管理")
public class WorkOrderRest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(WorkOrderRest.class);
    
    @Autowired
    private WorkOrderService workOrderService;
    
    @POST
    @Path("/effectiveProjectForWeChat")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增时，微信端获取有效的项目和房间", position = 4)
    public Response effectiveProjectForWeChat()
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
          EffectiveProjectResp resp = workOrderService.effectiveProjectForWeChat();
            map.setData(resp);
        }
        catch (Exception e)
        {
            LOGGER.error("effectiveProjectForWeChat error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/effectiveProjectForAdmin/{phone}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增时，admin获取有效的项目和房间")
    public Response effectiveProjectForAdmin(
        @ApiParam(value = "param", required = true) @PathParam("phone") String phone)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
          EffectiveProjectResp resp = workOrderService.effectiveProjectForAdmin(phone);
           map.setData(resp);
        }
        catch (Exception e)
        {
            LOGGER.error("effectiveProjectForAdmin error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/saveWorkOrder")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存工单信息")
    public Response saveWorkOrder(@ApiParam(value = "param", required = true) WorkOrderReq workOrderReq)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {   
            
            Integer id = workOrderReq.getId();
            if (null == id )
            {
                workOrderService.insertWorkOrder(workOrderReq);
                map.setResult(CommonResult.SUCCESS, "insert workOrde success");
            }
            else
            {
                workOrderService.editWorkOrder(workOrderReq);
                map.setResult(CommonResult.SUCCESS, "edit workOrde success");
            }           
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "saveWorkOrder", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/getWorkOrderDetail/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询工单详情")
    public Response getWorkOrderDetail(
        @ApiParam(value = "param", required = true) @PathParam("id") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            WorkOrderDetailResp resp = workOrderService.getWorkOrderDetail(id);
            
            map.setData(resp);
        }
        catch (Exception e)
        {
            LOGGER.error("getWorkOrderDetail error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/insertWorkOrderAppraise")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "客户评价")
    public Response insertWorkOrderAppraise(@ApiParam(value = "param", required = true) WorkOrderAppraise workOrderAppraise)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {   
                workOrderService.insertWorkOrderAppraise(workOrderAppraise);
                map.setResult(CommonResult.SUCCESS, "insert WorkOrderAppraise success");        
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "insertWorkOrderAppraise", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/getWorkOrderAppraise/{woId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询工单评价")
    public Response getWorkOrderAppraise(
        @ApiParam(value = "param", required = true) @PathParam("woId") Integer woId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            WorkOrderAppraise resp = workOrderService.getWorkOrderAppraise(woId);
           map.setData(resp);
        }
        catch (Exception e)
        {
            LOGGER.error("getWorkOrderAppraise error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @DELETE
    @Path("/deleteWorkOrder/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "删除工单")
    public Response deleteWorkOrder(
        @ApiParam(value = "param", required = true) @PathParam("id") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            workOrderService.deleteWorkOrder(id);
            map.setResult(CommonResult.SUCCESS, "delete workOrde success");
        }
        catch (Exception e)
        {
            LOGGER.error("deleteWorkOrder error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/getWorkOrderList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询工单列表")
    public Response getWorkOrderList(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<WorkOrderListResp> workOrderRespList = new ArrayList<WorkOrderListResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            workOrderRespList = workOrderService.getWorkOrderList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(workOrderRespList);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getWorkOrderList", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/getWorkOrderReportList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询工单报表列表")
    public Response getWorkOrderReportList(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<WorkOrderReportListResp> workOrderReportListResp = new ArrayList<WorkOrderReportListResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            workOrderReportListResp = workOrderService.getWorkOrderReportList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(workOrderReportListResp);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getWorkOrderReportList", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/downloadWorkOrderReport")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "下载工单报表excel")
    public Response downloadWorkOrderReport(@ApiParam(value = "param", required = true) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            String url = workOrderService.downloadWorkOrderReport(queryUtil);
            HashMap<String, String> resultmap = new HashMap<String, String>();
            resultmap.put("url", url);
            map.setData(resultmap);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "downloadWorkOrderReport", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    
    @POST
    @Path("/getWorkOrderListForWeChat")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询工单列表")
    public Response getWorkOrderListForWeChat()
    {
        CommonResultMap map = new CommonResultMap();
        List<WorkOrderListResp> workOrderRespList = new ArrayList<WorkOrderListResp>();
        try
        {
            workOrderRespList = workOrderService.getWorkOrderListForWeChat();
            map.setData(workOrderRespList);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getWorkOrderList", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}
